﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;


namespace KhoBanHang
{
    public partial class DonDatKH : Form
    {
        
        List<NhanVien> danhsachNVKD;
        List<KhachHang> danhsachKH;

        List<Business.DonDatKH> danhsachDDKH;

        List<HangHoa> danhSachHangHoa;

        Business.KhachHang thongtinKH;
        Business.DonDatKH DonDat;


        Business.HangHoa hangHoa;
        

      
        public DonDatKH()
        {
            InitializeComponent();
        }

        private void DonDatKH_Load(object sender, EventArgs e)
        {
            LoadDanhSachNhanVien();
            LoadDanhSachKhachHang();

            LoadDanhSachHang();
            LoadDanhsachDonDat(true);


            dtpickerTuNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerDenNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerTuNgayGiao.CustomFormat = "dd/MM/yyyy";

            dtpickerDenNgayGiao.CustomFormat = "dd/MM/yyyy";

            dtpickerNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayGiao.CustomFormat = "dd/MM/yyyy";

            txtNVDat.Text = "NV" + @Session.maNV;
        }

        private void LoadDanhSachNhanVien()
        {
            danhsachNVKD = Business.NhanVien.LayDanhSachNVKD();

            cbxNVDat.DataSource = danhsachNVKD;
            cbxNVDat.DisplayMember = "TenNV";

            cbxNVDat.ValueMember = "MaNVHienThi";
            cbxNVDat.SelectedItem = null;
        }

        private void LoadDanhSachKhachHang()
        {
            danhsachKH = Business.KhachHang.LayDanhSach();

            cbxKH.DataSource = danhsachKH;
            cbxKH.DisplayMember = "TenKH";
            cbxKH.ValueMember = "MaKHHienThi";

            cbxKH.SelectedItem = null;

            cbxKHlapdd.DataSource = danhsachKH;
            cbxKHlapdd.DisplayMember = "TenKH";
            cbxKHlapdd.ValueMember = "MaKHHienThi";

            cbxKHlapdd.SelectedItem = null;
        }

        private void LoadDanhSachHang()
        {
            danhSachHangHoa = Business.HangHoa.LayDanhSach();

            cbxTenHanglaphd.DataSource = danhSachHangHoa;
            cbxTenHanglaphd.DisplayMember = "TenHang";
            cbxTenHanglaphd.ValueMember = "MaHangHienThi";

            cbxTenHanglaphd.SelectedItem = null;
        }

        private void LoadDanhsachDonDat(bool lanDau)
        {
            if (lanDau)
            {
                danhsachDDKH = Business.DonDatKH.LayDanhSach();  
            }
            else 
            {
                if (rbtnKH.Checked && cbxKH.SelectedValue != null)

                    danhsachDDKH = Business.DonDatKH.LayDanhSach_KH(cbxKH.SelectedValue.ToString());
                else if (rbtnNVDat.Checked && cbxNVDat.SelectedValue != null)
                    danhsachDDKH = Business.DonDatKH.LayDanhSach_NVDat(cbxNVDat.SelectedValue.ToString());
                else if (rbtnNgayDat.Checked && dtpickerTuNgayDat.Value != null)

                    danhsachDDKH = Business.DonDatKH.LayDanhSach_NgayDat(dtpickerTuNgayDat.Value, dtpickerDenNgayDat.Value);
                else if (rbtnNgayGiao.Checked && dtpickerTuNgayGiao.Value != null)
                    danhsachDDKH = Business.DonDatKH.LayDanhSach_NgayGiao(dtpickerTuNgayGiao.Value, dtpickerDenNgayGiao.Value);
                else
                    danhsachDDKH = Business.DonDatKH.LayDanhSach();
            }

            gridviewDanhSach.DataSource = danhsachDDKH;
            gridviewDanhSach.Columns["MaKH"].Visible = false;
            gridviewDanhSach.Columns["MaDDH"].Visible = false;
            gridviewDanhSach.Columns["MaNV"].Visible = false;

            gridviewDanhSach.Columns["NgayDat"].DefaultCellStyle.Format = "dd/MM/yyyy";
            gridviewDanhSach.Columns["SoTien"].DefaultCellStyle.Format = "##,###,###";

            gridviewDanhSach.Columns["MaDDHHienThi"].HeaderText = "Mã đơn đặt";

            gridviewDanhSach.Columns["MaNVHienThi"].HeaderText = "Mã nhân viên";

            gridviewDanhSach.Columns["TenKH"].HeaderText = "Khách hàng";
            gridviewDanhSach.Columns["NgayDat"].HeaderText = "Ngày đặt";

            gridviewDanhSach.Columns["SoTien"].HeaderText = "Số tiền";

            gridviewDanhSach.Columns["TenKH"].HeaderText = "Khách hàng";
            gridviewDanhSach.Columns["GhiChu"].HeaderText = "Ghi chú";
            gridviewDanhSach.Columns["TenNV"].HeaderText = "Nhân viên đặt";
        }

        public void LoadDanhSachChiTietDonDat()
        {
            BindingSource source = new BindingSource();
            source.DataSource = DonDat.DanhSachChiTietDonDat;

            grivewChiTietLapdd.DataSource = source;
            grivewChiTietLapdd.RefreshEdit();

            grivewChiTietLapdd.Refresh();

            grivewChiTietLapdd.Columns["MaCTDDH"].Visible = false;

            grivewChiTietLapdd.Columns["MaHang"].Visible = false;

            grivewChiTietLapdd.Columns["MaDDH"].Visible = false;
            grivewChiTietLapdd.Columns["MaCTDDHHienThi"].HeaderText = "Mã chi tiết đặt";
            grivewChiTietLapdd.Columns["MaHangHienThi"].HeaderText = "Mã hàng";
            grivewChiTietLapdd.Columns["TenHang"].HeaderText = "Tên hàng";

            grivewChiTietLapdd.Columns["GiaBan"].HeaderText = "Giá bán";
            grivewChiTietLapdd.Columns["SoLuong"].HeaderText = "Số lượng";
            grivewChiTietLapdd.Columns["NgayGiao"].HeaderText = "Ngày giao";

            grivewChiTietLapdd.Columns["NgayGiao"].DefaultCellStyle.Format = "dd/MM/yyyy";
            grivewChiTietLapdd.Columns["GiaBan"].DefaultCellStyle.Format = "##,###,###";

            txtTongTien.Text = DonDat.TinhTongTienHang().ToString();
        }

        private void LoadThongTinDonDatHang(int maDDH)
        {
            DonDat = Business.DonDatKH.LayDonDatKH(maDDH);
            dtpickerNgayDat.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayDat.Text = DonDat.NgayDat.ToString();
            txtMaDD.Text = DonDat.MaDDHHienThi;

            txtNVDat.Text = DonDat.MaNVHienThi;
            txtNVDat.ReadOnly = true;
            txtGhiChu.Text = DonDat.GhiChu;


            cbxKHlapdd.DataSource = danhsachKH;
            cbxKHlapdd.DisplayMember = "TenKH";

            cbxKHlapdd.ValueMember = "MaKH";

            cbxKHlapdd.SelectedValue = DonDat.MaKH;
            LoadThongTinKH(DonDat.MaKH);

            LoadDanhSachChiTietDonDat();
        }

        private void LoadThongTinKH(int maKH)
        {
            thongtinKH = Business.KhachHang.LayThongTinChiTiet(maKH);
            txtDienThoailapdd.Text = thongtinKH.DienThoai;
            txtDiaChilapdd.Text = thongtinKH.DiaChi;
            txtMaSoThuelapdd.Text = thongtinKH.MaSoThue;
        }
        

       
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 0)
            {
                LoadDanhsachDonDat(false);
            }
        }

        private void cbxKHlapdd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxKHlapdd.SelectedItem != null && DonDat != null)
            {
                cbxKHlapdd.DisplayMember = "TenKH";

                cbxKHlapdd.ValueMember = "MaKH";
                int a = Int16.Parse(cbxKHlapdd.SelectedValue.ToString());
                LoadThongTinKH(a);
            }
        }

        private void cbxTenHanglaphd_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxTenHanglaphd.SelectedItem != null && DonDat != null)
            {
                hangHoa = danhSachHangHoa[cbxTenHanglaphd.SelectedIndex];

                txtSoLuongTon.Text = hangHoa.SoLuongTon.ToString();
                txtMahang.Text = hangHoa.MaHangHienThi.ToString();
                txtGiaBan.Text = hangHoa.GiaBan.ToString();
            }
        }
        

        
        private void btnTim_Click(object sender, EventArgs e)
        {
            LoadDanhsachDonDat(false);

        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (danhsachDDKH != null && danhsachDDKH.Count > 0)
            {
                tabControl1.SelectedTab = tabPage2;
                if (gridviewDanhSach.SelectedRows.Count == 0)
                {
                    int maDDH = danhsachDDKH[0].MaDDH;
                    LoadThongTinDonDatHang(maDDH);
                }
                else
                {
                    int maDDH = danhsachDDKH[gridviewDanhSach.SelectedRows[0].Index].MaDDH;
                    LoadThongTinDonDatHang(maDDH);
                }
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabPage2;
            dtpickerNgayDat.CustomFormat = "dd/MM/yyyy";

            DonDat = new Business.DonDatKH();

            XoaTrangChiTietDonDat();
            txtNVDat.Text = "NV" + @Session.maNV;
            grivewChiTietLapdd.DataSource = null;
            txtTongTien.Text = "";

            txtMaDD.Text = "";
            cbxKH.SelectedItem = null;

            txtDiaChilapdd.Text = "";
            txtDienThoailapdd.Text = "";
            txtGhiChu.Text = "";
            txtMaSoThuelapdd.Text = "";

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (DonDat.MaDDH == 0)
            {
                CapNhatThongTinChung();
                int maDDH = Business.DonDatKH.ThemMoi(DonDat);

                LoadThongTinDonDatHang(maDDH);
                MessageBox.Show("Thêm mới đơn đặt hàng thành công");
            }
            else
            {
                CapNhatThongTinChung();
                Business.DonDatKH.CapNhat(DonDat);

                LoadThongTinDonDatHang(DonDat.MaDDH);
                MessageBox.Show("Cập nhật đơn đặt hàng thành công");
            }

            XoaTrangChiTietDonDat();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (danhsachDDKH != null && danhsachDDKH.Count > 0)
            {
                if (gridviewDanhSach.SelectedRows.Count > 0)
                {
                    int maDDH = danhsachDDKH[gridviewDanhSach.SelectedRows[0].Index].MaDDH;
                    Business.DonDatKH.Xoa(maDDH);

                    LoadDanhsachDonDat(false);
                }
            }
        }

        private void CapNhatThongTinChung()
        {
            DonDat.MaKH = ((KhachHang)cbxKHlapdd.SelectedItem).MaKH;
            DonDat.TenKH = ((KhachHang)cbxKHlapdd.SelectedItem).TenKH;
            DonDat.MaNV = Int16.Parse(txtNVDat.Text.Substring(2));

            DonDat.NgayDat = dtpickerNgayDat.Value;

            DonDat.SoTien = double.Parse(txtTongTien.Text);
            DonDat.GhiChu = txtGhiChu.Text;
        }
        

        
        bool dangThemMoi = true;
        private void grivewChiTietLapdd_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ChiTietDonDatKH chiTietDonDat = DonDat.DanhSachChiTietDonDat[e.RowIndex];
            txtMahang.Text = chiTietDonDat.MaHang.ToString();
            txtGiaBan.Text = chiTietDonDat.GiaBan.ToString();
            txtSoLuongBan.Text = chiTietDonDat.SoLuong.ToString();

            cbxTenHanglaphd.SelectedValue = chiTietDonDat.MaHangHienThi;
            dtpickerNgayGiao.CustomFormat = "dd/MM/yyyy";
            dtpickerNgayGiao.Value = chiTietDonDat.NgayGiao;
            dangThemMoi = false;
        }

        private void XoaTrangChiTietDonDat()
        {
            cbxTenHanglaphd.SelectedItem = null;
            txtSoLuongTon.Text = "";
            txtSoLuongBan.Text = "";
            txtMahang.Text = "";

            txtGiaBan.Text = "";
            dtpickerNgayGiao.CustomFormat = "dd/MM/yyyy";
            dangThemMoi = true;
        }

        private void btnThemCTDD_Click(object sender, EventArgs e)
        {
            XoaTrangChiTietDonDat();
        }

        private void btnLuuCTDD_Click(object sender, EventArgs e)
        {
            int maDDH = DonDat.MaDDH;
            int maHang = Int16.Parse(txtMahang.Text.Substring(2));
            string tenHang = ((HangHoa)cbxTenHanglaphd.SelectedItem).TenHang;

            int soLuong = Int16.Parse(txtSoLuongBan.Text);

            double giaBan = double.Parse(txtGiaBan.Text);
            DateTime ngayGiao = dtpickerNgayGiao.Value;

            ChiTietDonDatKH chiTietDonDat = new ChiTietDonDatKH(DonDat.MaDDH, maHang, tenHang, soLuong, giaBan, ngayGiao);
            if (dangThemMoi == true)
            {
                DonDat.ThemMoiChiTietDonDat(maDDH, maHang, tenHang, soLuong, giaBan, ngayGiao);
            }
            else
            {
                if (grivewChiTietLapdd.SelectedRows.Count > 0)
                {
                    int index = grivewChiTietLapdd.SelectedRows[0].Index;

                    DonDat.CapNhatChiTietDonDat(index, maDDH, maHang, tenHang, soLuong, giaBan, ngayGiao);
                }
            }

            LoadDanhSachChiTietDonDat();
            XoaTrangChiTietDonDat();
        }

        private void btnXoaCTDD_Click(object sender, EventArgs e)
        {
            int index = grivewChiTietLapdd.SelectedRows[0].Index;
            DonDat.XoaChiTietDonDat(index);
            LoadDanhSachChiTietDonDat();
        }
        
    }
}
